class DingdansController < ApplicationController
  # GET /dingdans
  # GET /dingdans.xml
  def index
    @dingdans = Dingdan.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @dingdans }
    end
  end

  # GET /dingdans/1
  # GET /dingdans/1.xml
  def show
    @dingdan = Dingdan.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @dingdan }
    end
  end

  # GET /dingdans/new
  # GET /dingdans/new.xml
  def new
    if session[:yonghu]==nil
     redirect_to('/yonghus/new')
    else
    @dingdan = Dingdan.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @dingdan }
    end
    end
  end

  # GET /dingdans/1/edit
  def edit
    @dingdan = Dingdan.find(params[:id])
  end

  # POST /dingdans
  # POST /dingdans.xml
  def create
        @dingdan = Dingdan.new(params[:dingdan])
	session[:gouwuche] .collect {|x|
			if x.shuliang.to_i>0

			  newdingdan2shangpin=Dingdan2shangpin.new()
			   newdingdan2shangpin.shangpin=Shangpin.find(x.id)
			   newdingdan2shangpin.shuliang=x.shuliang.to_i
			   newdingdan2shangpin.save
			   @dingdan.dingdan2shangpins<<newdingdan2shangpin
		   end
		}
		@dingdan.yonghu=Yonghu.find(session[:yonghu] .id.to_i)

    respond_to do |format|
      if @dingdan.save
        flash[:notice] = 'Dingdan was successfully created.'
        format.html { redirect_to(@dingdan) }
        format.xml  { render :xml => @dingdan, :status => :created, :location => @dingdan }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @dingdan.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /dingdans/1
  # PUT /dingdans/1.xml
  def update
    @dingdan = Dingdan.find(params[:id])

    respond_to do |format|
      if @dingdan.update_attributes(params[:dingdan])
        flash[:notice] = 'Dingdan was successfully updated.'
        format.html { redirect_to(@dingdan) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @dingdan.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /dingdans/1
  # DELETE /dingdans/1.xml
  def destroy
    @dingdan = Dingdan.find(params[:id])
    @dingdan.destroy

    respond_to do |format|
      format.html { redirect_to(dingdans_url) }
      format.xml  { head :ok }
    end
  end
end
